﻿Imports System.Text
Imports D2L.Extensibility.AuthSdk
Imports RestSharp
Imports D2L.Extensibility.AuthSdk.Restsharp
Imports Teste.PoC.Valence.VB.Authenticate.Service

''' <summary>
''' Documentação: http://docs.valence.desire2learn.com/res/content.html
''' </summary>
''' <remarks></remarks>
<TestClass()>
Public Class CoursesTeste

    Private _authObj As Authenticate.Authenticate = New Authenticate.Authenticate()
    Protected method As Authenticate.Service

    'Documentado em: http://docs.valence.desire2learn.com/res/course.html
    'Protected Const ROUTE_COURSES_SCHEMA As String = "/d2l/api/lp/" + CoursesTeste.LP_VERSION + "/courses/schema"

    'http://docs.valence.desire2learn.com/res/course.html#Course.CourseOffering
    'http://docs.valence.desire2learn.com/res/course.html#get--d2l-api-lp-(version)-courses-(orgUnitId)
    'Protected Const ROUTE_COURSE_OFFERING As String = "/d2l/api/lp/" + CoursesTeste.LP_VERSION + "/courses/" + CoursesTeste.LMS_ORG_UNIT

    <TestMethod()>
    Public Sub courses_schema()

        method = New Authenticate.Service

        Dim _response As RestSharp.IRestResponse(Of Course.SchemaElements) = _authObj.ExecuteService_with_UserContext(method.GetCoursesSchema, RestSharp.Method.GET, Type.SchemaElements)

        Console.WriteLine("{0}{1}", "Response:", _response.ToString())
        Console.WriteLine("Response Content: {0}", _response.Content.ToString())

        Assert.IsNotNull(_response)
        Assert.AreEqual("OK", _response.StatusCode.ToString())

    End Sub
    '--Erro 403
    <TestMethod()>
    Public Sub courses_offering()

        method = New Authenticate.Service

        Dim _response As RestSharp.IRestResponse(Of Course.CourseOffering) = _authObj.ExecuteService_with_UserContext(method.GetCoursesOffering + "6606", RestSharp.Method.GET, Type.CourseOffering)

        Console.WriteLine("{0}{1}", "Response:", _response.ToString())
        Console.WriteLine("Response Content: {0}", _response.Content.ToString())

        Assert.IsNotNull(_response)
        Assert.AreEqual("OK", _response.StatusCode.ToString())

    End Sub


    '[MF] - Deserialize a collection
    <TestMethod()>
    Public Sub courses_schema_deserialize_test()

        'Dim _servico As String = CoursesTeste.ROUTE_COURSES_SCHEMA
        'Dim _valenceAppContext As ID2LAppContext
        'Dim _valenceHost As HostSpec
        'Dim _valenceUserContext As ID2LUserContext
        'Dim _appFactory As D2LAppContextFactory
        'Dim _client As RestClient
        'Dim _request As RestRequest
        'Dim _authenticator As ValenceAuthenticator
        'Dim _response As IRestResponse(Of Course.SchemaElements) '----

        '_appFactory = New D2LAppContextFactory()
        '_request = New RestRequest(_servico)
        '_client = New RestClient(CoursesTeste.LMS_URL_HTTPS)
        '_valenceAppContext = _appFactory.Create(CoursesTeste.APP_ID, CoursesTeste.APP_KEY)
        '_valenceHost = New HostSpec("https", CoursesTeste.LMS_URL, CoursesTeste.LMS_PORTA)
        '_valenceUserContext = _valenceAppContext.CreateUserContext(CoursesTeste.USER_ID, CoursesTeste.USER_KEY, _valenceHost)

        '_authenticator = New ValenceAuthenticator(_valenceUserContext)
        '_authenticator.Authenticate(_client, _request)

        '_response = _client.Execute(Of Course.SchemaElements)(_request) '----

        'Console.WriteLine("{0}{1}", CoursesTeste.LMS_URL_HTTPS, _servico)

        'Dim listOfCourses As Course.SchemaElements
        'listOfCourses = Deserialize(_response.Content)

        'Console.WriteLine("Number of courses: {0}", listOfCourses.Count)

        'Assert.IsNotNull(listOfCourses)

    End Sub

    '[MF] - Metodo para deserializar uma lista de cursos. 
    'Fiz como um exemplo, acredito ser possivel tornar este metodo generico de tipo e utiliza-lo 
    'na descerializacao de todas as colecoes
    Private Function Deserialize(ByVal courseCollection As Object) As Course.SchemaElements

        Dim returnObject As New Course.SchemaElements ' List(Of Course.SchemaElement)
        Dim jsSer As New System.Web.Script.Serialization.JavaScriptSerializer 'Pertence a biblioteca System.Web.Extentions
        returnObject.AddRange(jsSer.Deserialize(Of List(Of Course.SchemaElement))(courseCollection))

        Return returnObject

    End Function

End Class
